package blueBridgeCup;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class demo8 {
    static int sx, sy, ex, ey, m, n;
    static boolean[][] visited;
    static int[]dx={0,0,1,-1};
    static int[]dy={1,-1,0,0};
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        m = in.nextInt();
        n = in.nextInt();
        int[][] G = new int[m][n];
visited=new boolean[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                G[i][j] = in.nextInt();
            }
        }
        sx = in.nextInt() - 1;
        sy = in.nextInt() - 1;
        ex = in.nextInt() - 1;
        ey = in.nextInt() - 1;
        in.close();

        System.out.println(bfs(sx,sy,G));


    }

    private static int bfs(int sx, int sy, int[][] G) {
        Queue<int[]>queue=new LinkedList<>();
        queue.offer(new int[]{sx,sy,0});
        visited[sx][sy]=true;
        while (!queue.isEmpty()){
            int[]poll= queue.poll();
            int i=poll[0];
            int j=poll[1];
            int step=poll[2];
            if(i==ex&&j==ey)return step;
            for (int k = 0; k <4 ; k++) {
                int x=i+dx[k];
                int y=j+dy[k];
                if(x>=0&&x<m&&y>=0&&y<n&&!visited[x][y]&&G[x][y]==1){
                 queue.offer(new int[]{x,y,step+1});
                 visited[x][y]=true;
                }
            }

        }
return -1;
    }
}
